package com.demo1112.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/demo1112/demo02.do")
public class Demo02Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //Cookie获取
        Cookie[] cookies = req.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            System.out.println("cookie的name = " + cookie.getName());
            System.out.println("cookie的value = " + cookie.getValue());
            System.out.println("cookie的tDomain = " + cookie.getDomain());
            System.out.println("cookie的MaxAge = " + cookie.getMaxAge());
            System.out.println("cookie的Path = " + cookie.getPath());
        }

        //通过cookie名称 找到对应的cookie
        String name = "key1";
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (name.equals(cookie.getName())){
                System.out.println("找到对应的cookie");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
